window.onload = function () {
    if (!document.getElementsByClassName) {
        document.getElementsByClassName = function (cls) {
            var ret = [];
            var els = document.getElementsByTagName('*');
            for (var i = 0, len = els.length; i < len; i++) {
                if (els[i].className === cls
                    || els[i].className.indexOf(cls + ' ') >= 0
                    || els[i].className.indexOf(' ' + cls + ' ') >= 0
                    || els[i].className.indexOf(' ' + cls) >= 0) {
                    ret.push(els[i]);
                }
            }
            return ret;
        }
    }

    var cartTable = document.getElementById('payTable');
    var tr = cartTable.getElementsByTagName("ul");
    var priceTotal = document.getElementById('pay-sum');
    var consigneeId=document.getElementsByClassName('consigneeId');
    var address=document.getElementsByClassName('user-addresslist');
    
    for(var i = 0;i < address.length; i++) {
    	address[i].getElementsByTagName('input')[0].style.display="none";
    	address[i].style.border="thick dotted black";
    	address[0].style.border="thick dotted red";
    	address[i].onclick = function () {
    		var consigneeId=this.getElementsByTagName('input')[0];
    		consigneeId.checked=true;
    		for(var i = 0;i < address.length; i++) {
    			address[i].style.border="thick dotted black";
    		}
    		this.style.border="thick dotted red";
    	}
    }
    
    //计算
    function getTotal() {
        var price = 0;
        var HTMLstr = '';
        for (var i = 0, len = tr.length; i < len; i++) {
                price += parseFloat(tr[i].getElementsByTagName("li")[4].innerHTML);
        }

        priceTotal.innerHTML = price.toFixed(2);
    }

    //小计
    function getSubTotal(tr) {
        var tds = tr.getElementsByTagName("li");
        var price = parseFloat(tds[2].innerHTML);
        var count = parseInt(tr.getElementsByTagName('input')[1].value);
        //price.style.display="none";
        //count.style.display="none";
        //tds[1].innerHTML=price;
        var SubTotal = parseFloat(price * count);
        tds[4].innerHTML = SubTotal.toFixed(2);
    }
  
    for (var i = 0; i < tr.length; i++) {
    	getSubTotal(tr[i]);
    	getTotal();
        tr[i].onclick = function (e) {
            e = e || window.event;
            var el = e.srcElement;
            var cls = el.className;
            var input = this.getElementsByTagName('input')[1];
            var val = parseInt(input.value);
            switch (cls) {
                case 'add am-btn':
                    if(val > 99){
                    	input.value = 99;
                    }
                    getSubTotal(this);  
                    
                    var id=this.getElementsByTagName('input')[3];
                    var count=parseInt(input.value);
                    window.location.href="updateShoppingCarCountInOrderAction.action?updateShoppingCarId="+id.value+"&updateShoppingCarCount="+count;
                  
                    break;
                case 'min am-btn':
                    if (val < 1) {
                        input.value = 1;
                    }
                    getSubTotal(this);
                    
                    var id=this.getElementsByTagName('input')[3];
                    var count=parseInt(input.value);
                    window.location.href="updateShoppingCarCountInOrderAction.action?updateShoppingCarId="+id.value+"&updateShoppingCarCount="+count;
                  
                    break;
                default :
                    break;
            }
            getTotal();
        }
        tr[i].getElementsByTagName('input')[1].onkeyup = function () {
            var val = parseInt(this.value);
            var tr = this.parentNode.parentNode.parentNode.parentNode.parentNode
            if (isNaN(val) || val < 1) {
                val = 1;
            }
            else if(val > 99){
            	val = 99;
            }
            this.value = val;
            getSubTotal(tr);
            getTotal();
            
            var id=tr.getElementsByTagName('input')[3];
            window.location.href="updateShoppingCarCountInOrderAction.action?updateShoppingCarId="+id.value+"&updateShoppingCarCount="+val;
            
        }
    }
}

